<?php
/**
 * Logo_Controller
 * - Controller for module page Logo
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_DAO_DIR . "/MQuangcao.class.php");
class Quangcao_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuQC = true;
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new ViewAdmin ();
        $this->tpl_title = 'Danh sách quáng cáo';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Quản lý quảng cáo';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbQuangcao = new MQuangcao();
    	if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
	        
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$dataQC = $ojbQuangcao->getByPrimaryKey($dataPost['itemID']);
	        	$ojbQuangcao->setAttribute('id', $dataPost['item']);
	        	$ojbQuangcao->delete();
	        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataQC['code']);
	        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataQC['code']);
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	    				$dataQC = $ojbQuangcao->getByPrimaryKey($aP);
	        			$ojbQuangcao->setAttribute('id', $aP);
	        			$ojbQuangcao->delete();
	        			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataQC['code']);
	        			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataQC['code']);
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbQuangcao->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq)), false, true);
	        	$idMin = $ojbQuangcao->getIdBySeq($seqMin);
	        	
	        	$ojbQuangcao->setAttribute('seq', $seqMin);
	        	$ojbQuangcao->update(array('id' => $id));
	        	$ojbQuangcao->emptyAttribute();
	        	$ojbQuangcao->setAttribute('seq', $seq);
	        	$ojbQuangcao->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbQuangcao->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq)), true);
	        	$idMax = $ojbQuangcao->getIdBySeq($seqMax);
	        	
	        	$ojbQuangcao->setAttribute('seq', $seqMax);
	        	$ojbQuangcao->update(array('id' => $id));
	        	$ojbQuangcao->emptyAttribute();
	        	$ojbQuangcao->setAttribute('seq', $seq);
	        	$ojbQuangcao->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbQuangcao->getMinMax(array(), true);
        $this->minseq = $ojbQuangcao->getMinMax(array(), false, true);
        
    	// List all
    	$arrParam = array('m' => 'quangcao');
    	$arrField = array('id', 'seq', 'code', 'url');
        $condition = array();
		
        $data = FunctionCommon::paging($ojbQuangcao, $arrParam, $arrField, $condition);
        $arrContent = $data["data"];
		$this->arrContent = array();
		$this->currPage = '';
		$this->numberPage = '';
		$this->strPaging = '';
		if (count ($arrContent) > 0) {
			foreach ($arrContent as $content) {
				$this->arrContent[] = $content;
			}
			$this->currPage = $data["currPage"];
			$this->numberPage = $data["numberPage"];
			$this->strPaging = $data["strPaging"];
		}        

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin ();
        $this->tpl_title = 'Sửa tên danh mục';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Sửa quảng cáo Add/Edit';
        $this->tpl_button = "Thêm mới";
        $this->arrField = array();
        $this->id = '';
        $this->tenFile = '';
        $this->dialog = false;
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
    	$ojbQuangcao = new MQuangcao();
    	$this->arrField = array('url'=>'', 'nameimage'=>'', 'itemID'=>'', 'mode'=>'', 'type'=>'');
    	if ($this->isPost()) {
        	$arrPostValue = FunctionCommon::getPost(array_keys($this->arrField));
        	
        	$this->id = $arrPostValue['itemID'];
        	
        	if ($arrPostValue['mode'] != '') {
        		
        		// Update du lieu
        		if ($arrPostValue['type'] == 'edit' && $this->id != '') {
        			$ojbQuangcao->setAttribute('code', $arrPostValue['nameimage']);
        			$ojbQuangcao->setAttribute('url', $arrPostValue['url']);
		        	$ojbQuangcao->update(array('id' => $this->id));
		        	$this->dialog = true;
        		}
        		
        		// Insert du lieu
	        	if ($arrPostValue['type'] == 'edit' && $this->id == '') {
		    		$ojbQuangcao->setAttribute('seq', $ojbQuangcao->getMinMax(array(), true) + 1);
		    		$ojbQuangcao->setAttribute('code', $arrPostValue['nameimage']);
		    		$ojbQuangcao->setAttribute('url', $arrPostValue['url']);
		        	$ojbQuangcao->insert();
		        	$this->dialog = true;
		        }
        		
        		// Xu ly file
	        	if ($arrPostValue['type'] == 'upload') {
	        		$this->tenFile = FunctionCommon::uploadFileTo($_FILES["code"], "img-the", $arrPostValue['nameimage']);
	        		if ($this->tenFile == false) {
	        			$this->tenFile = $arrPostValue['nameimage'];
	        		}
		        }
		        
		        // Delete image
	        	if ($arrPostValue['type'] == 'delete') {
		        	$tenFile = $arrPostValue['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$tenFile);
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$tenFile);
		        }
		        
		        $this->arrField = $arrPostValue;
        	} else {
        		$this->tpl_title = 'Sửa Logo';
        		$this->tpl_button = "Sửa";
	        	$this->arrField = $ojbQuangcao->getByPrimaryKey($this->id);
	        	$this->tenFile = $this->arrField["code"];
        	}
    	}

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

